from pynput.keyboard import Key, Controller  # 导入按键控制
import time  # 导入时间包，调用sleep函数让琴键有时间间隔
import threading  # 导入线程包


# 主旋律
def theme(right):
    for n in right:
        if n != " ":
            keyboard.press(n)
            time.sleep(0.5)
            keyboard.release(n)

        else:
            time.sleep(0.5)


# 和旋
def chord(left):
    for n in left:
        if n != " ":
            keyboard.press(n)
            time.sleep(0.5)
            keyboard.release(n)
        else:
            time.sleep(0.5)


if __name__ == '__main__':
    # 准备演奏
    # 控制键盘
    keyboard = Controller()
    # 切换到vue键盘钢琴(auto piano)网页
    keyboard.press(Key.alt)
    time.sleep(1)
    keyboard.press(Key.tab)
    keyboard.release(Key.tab)
    keyboard.release(Key.alt)
    time.sleep(2)
    # 右手
    right = "sshhjjh ggffdds hhggffd hhggffd sshhjjh ggffdds"
    # 左手
    left = "ttttttt ttttrrt  ttttttr ttttttr ttttttt ttttrrt"
    # 运行线程
    rt = threading.Thread(target=theme, args=(right,))
    lt = threading.Thread(target=chord, args=(left,))
    rt.start()
    lt.start()
